// pages/home/classify/classify.js

// template/template.js

var app = getApp();
var indexNumber = ""; //播放节目下标
var id  = ""; //获取分类id
var startPoint = 1; //起始数据标记
var dataParm = {}; //封装请求数据
var page;  //分页操作数据
Page({
  /**
   * 页面的初始数据
   */
  data: {
	  audioIndex: 0,//当前播放下标
	  pauseStatus: true, //持续状态
	  timer: '',//播放时间 播放时间统计
	  currentPosition: 0, //当前位置 进度条
	  duration: 0,   //持续时间进度条使用
	  free:true,
	  play: true,

	  url:""
  },


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this;
		id = options.id;
		wx.setNavigationBarTitle({ //更改标题栏显示
			title: options.classify
		})

		//调用查询数据列表方法
		dataParm = { categoryId: id, startPoint: startPoint, maxPage: 10}
		var url = "programResource/freeProgram/" + id;
		that.setData({url:url})
		that.getDataList(url, dataParm);
		
	},

	/**
	 * 获取数据列表
	 */
	getDataList: function (url, dataParm){
		var that = this;
		console.log(dataParm)
		app.requestLoading(app.globalData.https + url,
			"get", dataParm, "加载中...", function (res) {
				if (res.code == 0) {
					var classfilyDataList = res.data.result;
					if (classfilyDataList.length>0){
						console.log(classfilyDataList)
						// 获取系统信息  
						that.setData({
							classfilyDataList: classfilyDataList
						});
						var dt = res.data;
						console.log(dt)
						page = { pageSize: dt.pageSize, startPage: dt.startPage, totalPages: dt.totalPages, totalResults: dt.totalResults}

					}else{
						wx.showToast({
							title: '此分类暂无节目',
							icon: 'none',
							duration: 2000
						})
					}
				}
			}
		)
	},

  /**
   * 获取免费节目列表
   */
  free: function () {
	  //调用查询数据列表方法
	  dataParm = { categoryId: id, startPoint: startPoint, maxPage: 10 }
	  var url = "programResource/freeProgram/" + id;
	var that = this;
    this.setData({
      free: true,
      pay: false,
	  url:url
    })
	  
	  that.getDataList(url, dataParm);
  },

  /**
   * 获取收费的节目列表
   */
  pay: function () {
	  dataParm = { categoryId: id, startPoint: startPoint, maxPage: 10 }
	  var url = "programResource/chargeProgram/" + id;
	var that = this;
    this.setData({
      free: false,
      pay: true,
	  url :url
    })
	  //调用查询数据列表方法
	  that.getDataList(url, dataParm);
  },

  play: function (e) {
	  console.log("播放")
	  var that = this;
	  indexNumber = e.currentTarget.dataset.indexnumber;
	  that.setData({
		  indexNumber: indexNumber,
		  pauseStatus: true 
	  }) 
	  let audioList = this.data.classfilyDataList
		wx.playBackgroundAudio({
			dataUrl: audioList[indexNumber].playUrl,
			title: audioList[indexNumber].title,
			coverImgUrl: audioList[indexNumber].imageUrl
		})
	  console.log(audioList[indexNumber].image)
		let timer = setInterval(function () {
			that.setDuration(that)
		}, 1000)
		this.setData({ timer: timer }) 
  },
	setDuration(that) {
		wx.getBackgroundAudioPlayerState({
			success: function (res) {
				var status = res.status
				var duration = res.duration
				var currentPosition = res.currentPosition
				if (status === 1 || status === 0) {
					that.setData({
						currentPosition: that.stotime(currentPosition),
						duration: that.stotime(duration),
						sliderValue: Math.floor(currentPosition * 100 / duration),
					})
				} 
			}
		})
	},
	stotime: function (s) {
		let t = '';
		if (s > -1) {
			// let hour = Math.floor(s / 3600);
			let min = Math.floor(s / 60) % 60;
			let sec = s % 60;
			// if (hour < 10) {
			//   t = '0' + hour + ":";
			// } else {
			//   t = hour + ":";
			// }

			if (min < 10) { t += "0"; }
			t += min + ":";
			if (sec < 10) { t += "0"; }
			t += sec;
		}
		return t;
	},

	/**
	* 监听button暂停按钮
	*/
	stop: function (e) { console.log("暂停")
		var that = this;
		that.setData({
			indexNumber: -1, //标记播放按钮转换
			pauseStatus: false //播放状态
		})
		wx.pauseBackgroundAudio();
  },
    search:function(){
        wx.navigateTo({
            url: "../../search/search",
            success: function () {
                console.log('跳转到news页面成功')// success              
            },
            fail: function () {
                console.log('跳转到news页面失败')  // fail 
            }
        })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

	  var that= this;
	  var url = that.data.url;
	  console.log(url)
	  if (page.startPage - 1 > 0) {
		  	startPoint = page.startPage - 1 * page.pageSize
			console.log(startPoint+"下拉")
			dataParm = { categoryId: id, startPoint: startPoint, maxPage: 10 }
			that.getDataList(url, dataParm);
			console.log(url)
	  }else{
		  console.log("到顶了。。。。")
	  }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	  var that = this; 
	  var url = that.data.url;
	  console.log(url)
	  if (page.startPage + 1 <= page.totalPages) {
		  	startPoint = page.startPage + 1 * page.pageSize
			console.log(startPoint + "上拉")
			dataParm = { categoryId: id, startPoint: startPoint, maxPage: 10 }
			that.getDataList(url, dataParm);
			console.log(url)
	  }else{
		  console.log("无新数据")
	  }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  playButtonClickURL: function (e) {
    var index = e.target.dataset['index']; //当前操作对象的 index
    console.log("点击了播放" + index)
    this.setData({
      playBthidShow: "play"
    })
    wx.navigateTo({
      url: "../pages/play_audio/play_audio",
      success: function () {
        console.log('跳转到news页面成功')// success              
      },
      fail: function () {
        console.log('跳转到news页面失败')  // fail 
      }
    })
  },
  playButtonClick: function (e) {
    var index = e.target.dataset['index']; //当前操作对象的 index
    console.log("点击了播放不跳转" + index)
    console.log(this.data.nrList[e.target.dataset.index])
    this.setData({
      clickId: index, //选中的下标值
    })

  }

})
